package cn.configure;
import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.*;
import java.nio.charset.StandardCharsets;
/**
 * 二维码内容参数
 *
 * @author xbsoft2
 */
@Data
public final class QRContentsParamsConfigure implements Serializable {
  private static final long serialVersionUID = 1L;
  /**
   * 配置缓存常量
   */
  private static QRContentsParamsConfigure qrContentsParamsConfigure;
  /**
   * 配置信息保存目录
   */
  private static final String QR_CONTENTS_PARAMS_CONFIGURE_DIRECTORY = "./configure";
  /**
   * 配置信息文件名
   */
  private static final String QR_CONTENTS_PARAMS_CONFIGURE_NAME = "qrContentsParamsConfigure.json";
  private QRContentsParamsConfigure() {
    this.url = "http://a2.4000063966.com/q.do";
    this.isEnableUrl = Boolean.TRUE;
    this.key1 = "v";
    this.value1 = "2";
    this.isEnable1 = Boolean.TRUE;
    this.key2 = "";
    this.value2 = "";
    this.isEnable2 = Boolean.FALSE;
    this.key3 = "";
    this.value3 = "";
    this.isEnable3 = Boolean.FALSE;
    this.key4 = "";
    this.value4 = "";
    this.isEnable4 = Boolean.FALSE;
    this.key5 = "";
    this.value5 = "";
    this.isEnable5 = Boolean.FALSE;
    this.key6 = "s";
    this.startNum = 0;
    this.endNum = 0;
    this.isText = Boolean.TRUE;
    this.isEnable6 = Boolean.TRUE;
    this.key7 = "";
    this.startPort = 0;
    this.endPort = 0;
    this.isSuffix = Boolean.FALSE;
    this.isEnable7 = Boolean.FALSE;
    this.timeSpacing = 0;
  }
  private String url;
  private Boolean isEnableUrl;
  private String key1;
  private String value1;
  private Boolean isEnable1;
  private String key2;
  private String value2;
  private Boolean isEnable2;
  private String key3;
  private String value3;
  private Boolean isEnable3;
  private String key4;
  private String value4;
  private Boolean isEnable4;
  private String key5;
  private String value5;
  private Boolean isEnable5;
  private String key6;
  private Integer startNum;
  private Integer endNum;
  private Boolean isText;
  private Boolean isEnable6;
  private String key7;
  private Integer startPort;
  private Integer endPort;
  private Boolean isSuffix;
  private Boolean isEnable7;
  private Integer timeSpacing;
  /**
   * 获取配置信息
   *
   * @return 配置信息
   * @throws IOException            IOException
   * @throws ClassNotFoundException 异常
   */
  public static QRContentsParamsConfigure get() throws IOException, ClassNotFoundException {
    // 先从缓存常量中获取配置信息
    if (qrContentsParamsConfigure != null) {
      return qrContentsParamsConfigure;
    }
    // 从配置文件中获取缓存信息
    File directory = new File(QR_CONTENTS_PARAMS_CONFIGURE_DIRECTORY);
    if (!directory.exists()) {directory.mkdirs();}
    File configFile = new File(QR_CONTENTS_PARAMS_CONFIGURE_DIRECTORY + "/" + QR_CONTENTS_PARAMS_CONFIGURE_NAME);
    if (configFile.exists()) {
      BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(configFile)), StandardCharsets.UTF_8));
      String line;
      StringBuilder jsonString = new StringBuilder();
      while ((line = reader.readLine()) != null) {
        jsonString.append(line);
      }
      reader.close();
      return qrContentsParamsConfigure = JSON.parseObject(jsonString.toString(), QRContentsParamsConfigure.class);
    }
    // 返回默认配置信息
    return qrContentsParamsConfigure = new QRContentsParamsConfigure();
  }
  /**
   * 保存配置信息
   *
   * @throws IOException 异常
   */
  public static void save() throws IOException {
    File file = new File(QR_CONTENTS_PARAMS_CONFIGURE_DIRECTORY + "/" + QR_CONTENTS_PARAMS_CONFIGURE_NAME);
    PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), StandardCharsets.UTF_8)), Boolean.TRUE);
    printWriter.println(JSON.toJSONString(qrContentsParamsConfigure));
    printWriter.close();
  }
}
